<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file used for print email.
 *
 * Version: 1.4
 *
 * @package   block_email_list
 * @author    Toni Mas <toni.mas@uib.es>
 * @copyright 2009 by <toni.mas at uib dot es>
 * @license   The source code packaged with this file is Free Software, Copyright (C) 2009 by
 *            <toni.mas at uib dot es>.
 *            It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
 *            You can get copies of the licenses here: http://www.affero.org/oagpl.html
 *            AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
 */
global $CFG, $OUTPUT;

require_once('../../../config.php');
require_once($CFG->dirroot.'/blocks/email_list/email/lib.php');

// For apply ajax and javascript functions.
require_once($CFG->libdir. '/ajax/ajaxlib.php');
require_once($CFG->dirroot.'/blocks/email_list/email/email.class.php');

//require_js('treemenu.js');
//require_js('email.js');

// SSL encription
if ($CFG->email_enable_ssl) {
    $PAGE->https_required();
}

$mailid 	= required_param('id', PARAM_INT); 			// email ID
$courseid	= optional_param('course', SITEID, PARAM_INT); 				// Course ID
$action 	= optional_param('action', '', PARAM_ALPHANUM); 	// Action to execute
$folderid	= optional_param('folderid', 0, PARAM_INT); 		// folder ID

$mails 		= optional_param('mails', '', PARAM_ALPHANUM); 	// Next and previous mails
$selectedusers = optional_param('selectedusers', '', PARAM_ALPHANUM); // User who send mail

// If defined course to view
if (! $course = $DB->get_record('course', array('id' => $courseid))) {
    print_error('invalidcourseid', 'block_email_list');
}

if ($course->id == SITEID) {
    $coursecontext = context_system::instance();   // SYSTEM context
} else {
    $coursecontext = context_course::instance($course->id);   // Course context
}

// eMail
$email = new eMail();
$email->set_email($mailid);

require_login($course->id, false); // No autologin guest

if (! email_is_enabled_email_list($course->id)) {
    print_error('blocknotavailable', 'block_email_list');
}

// Add log for one course
add_to_log($course->id, 'email_list', 'view mail', 'view.php?id='.$mailid,
           'View mail '.$email->subject, 0, $USER->id);


/// Print the page header

if (has_capability('block/email_list:editsettings', $coursecontext)) {
    $preferencesbutton = email_get_preferences_button($courseid);
    $PAGE->set_button($preferencesbutton);
}

$stremail  = get_string('name', 'block_email_list');
// Add subject on information page
$stremail .= ' :: '.$email->subject;

$url = '/blocks/email_list/email/view.php?id='. $mailid;
if ($courseid != SITEID) {
    $url .= '&amp;course='. $courseid;
}
if ($action != '') {
    $url .= '&amp;action='. $action;
}
if ($folderid != 0) {
    $url .= '&amp;folderid='. $folderid;
}
if ($mails != '') {
    $url .= '&amp;mails='. $mails;
}

$PAGE->set_title("$course->shortname: $stremail");
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->set_url($url);
$PAGE->navbar->add(get_string('nameplural', 'block_email_list'), 'index.php?id='.$course->id);
$PAGE->navbar->add(get_string('name', 'block_email_list'));
$PAGE->requires->css('/blocks/email_list/email/email.css');
$PAGE->requires->css('/blocks/email_list/email/treemenu.css');
$PAGE->requires->css('/blocks/email_list/email/tree.css');
$PAGE->requires->js('/blocks/email_list/email/treemenu.js');
$PAGE->requires->js('/blocks/email_list/email/email.js');

echo $OUTPUT->header();

// Options
$options = new stdClass();
$options->id = $mailid;
$options->mailid = $mailid;
$options->course = $courseid;
$options->folderid = $folderid;

/// Prepare url's to sending
$baseurl = email_build_url($options);

/// Print the main part of the page

// Print principal table. This have 2 columns . . .  and possibility to add right column.
echo '<table id="layout-table">
  			<tr>';


// Print "blocks" of this account
echo '<td style="width: 180px;" id="left-column">';

// HACK for print folder links correct
$options->id = $courseid;
email_printblocks($USER->id, $courseid);
$options->id = $mailid;

// Close left column
echo '</td>';

// Print principal column
echo '<td id="middle-column">';

// Get actual folder, for show
if (! $folder = email_get_folder($folderid)) {
    // Default, is inbox
    $folder = new stdClass();
    $folder->name = get_string('inbox', 'block_email_list');
}

// Print middle table
echo $OUTPUT->heading(get_string('mailbox', 'block_email_list'). ': '. $folder->name);

echo '<div>&#160;</div>';

unset($options->id);
// Print tabs options
email_print_tabs_options($courseid, $folderid, $action);

// Print action in case . . .
// Get user, for show this fields
if (! $user = $DB->get_record('user', array('id' =>$USER->id))) {
    $OUTPUT->notification(get_string('failreadinguser', 'block_email_list'));
}

// Prepare next and previous mail
if ($mails) {
    $urlnextmail  = '';
    $next = email_get_nextprevmail($mailid, $mails, true);

    if ($next) {
        $action = (PHP_VERSION < 5) ? $options : clone($options);	// Thanks Ann
        $action->id = $next;
        $urlnextmail  = email_build_url($action);
        $urlnextmail .= '&amp;mails='. $mails;
        $urlnextmail .=  '&amp;action='.EMAIL_VIEWMAIL;
    }

    $urlpreviousmail  = '';
    $prev = email_get_nextprevmail($mailid, $mails, false);

    if ( $prev ) {
        $action = (PHP_VERSION < 5) ? $options : clone($options);	// Thanks Ann
        $action->id = $prev;
        $urlpreviousmail  = email_build_url($action);
        $urlpreviousmail .= '&amp;mails='. $mails;
        $urlpreviousmail .= '&amp;action='.EMAIL_VIEWMAIL;
    }
}

$email->display($courseid, $folderid, $urlpreviousmail, $urlnextmail, $baseurl, $user,
                has_capability('moodle/site:viewfullnames', $coursecontext));

// Close principal column
echo '</td>';

// Close table
echo '</tr> </table>';

echo $OUTPUT->footer();
